package com.yl.applicationsecurity.utils;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.util.HashMap;
import java.util.List;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yl.applicationsecurity.pojo.ApplicationCoverage;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Map;

/**
 * @author 鹤仙问鹿仙
 * @program: Application-security
 * @description:  拉取UAT环境的appname
 * @date 2025-04-19 15:39:12
 */
public class AppnameClientUtil {


    private final static String uri = "https://ylops.jtexpress.com.cn/api/open/get_app_info_by_product_env/";

    private final static String uriUat = "https://uat-ylops.jnt-express.com.cn/api/open/get_app_info_by_product_env/";
    /**
     * 正式环境密钥
     */
    private final static String Authorization = "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoyMDU2MjA0Nzk5LCJqdGkiOiIwYzk0NGU0YjFjZjc0YmM2YjNiYmNhOWQzMWY5YTRmYiIsInVzZXJfaWQiOjN9.1N5RbG4_U-YD5QYxXKeUtOGKKXN2ITiYx4-nCZPpvKs";

    /**
     * UAT环境密钥
     */
    private final static String AuthorizationUat = "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoyMDU2MjA0Nzk5LCJqdGkiOiI4MTMwZGExZWM2YzU0NjdjYjAyNWQwNjliZmIzMGU0YyIsInVzZXJfaWQiOjN9.3C3OJs_HSZedQ-9eZDBoMobQydzyf8oNzee25WApxtA";

    private final static Map<String, String> productNameMap = new HashMap<>();



    public static List<ApplicationCoverage> getAppNameList() throws IOException {

        //productNameMap.put("devops", "平台运维");
        productNameMap.put("jmsidn", "印尼JMS系统");
        productNameMap.put("jmseg", "埃及JMS系统");
        productNameMap.put("jmsbr", "巴西JMS系统");
        productNameMap.put("jmsmx", "墨西哥JMS系统");
        productNameMap.put("jms", "中国JMS系统");
        productNameMap.put("jmssa", "沙特JMS系统");
        productNameMap.put("yms", "中国纭毅系统");
        productNameMap.put("jmssg", "新加坡JMS系统");
        productNameMap.put("jtm", "国际标快系统(JTM)");
        productNameMap.put("jmsth", "泰国JMS系统");
        productNameMap.put("jmsph", "菲律宾JMS系统");
        productNameMap.put("jmsmy", "马来西亚JMS系统");
        productNameMap.put("jmssea", "东南亚数据中台");
        productNameMap.put("dc", "中国-数据中台");
        //productNameMap.put("bigdata", "大数据");
        productNameMap.put("jmsvn", "越南JMS系统");
        //productNameMap.put("jti", "极兔国际G-G项目");
        //productNameMap.put("dts", "数据同步");
        productNameMap.put("wcloud", "白云(共配系统)");
        productNameMap.put("jmsintal", "国际官网");
       // productNameMap.put("alg", "机器学习");
        //productNameMap.put("jti-crsw", "极兔国际-CRS-CRW");
        productNameMap.put("jmstj", "兔集APP即决策APP");
        productNameMap.put("jmscb", "柬埔寨JMS系统");
        productNameMap.put("ymsidn", "印尼纭毅系统");
        productNameMap.put("ymsth", "泰国纭毅系统");
        productNameMap.put("ymsvn", "越南纭毅系统");
        productNameMap.put("sands", "沙滩说");
        productNameMap.put("jty", "集运系统");
        productNameMap.put("upfos", "UPFOS");
        //productNameMap.put("jis", "云路内部系统");
        productNameMap.put("cwh", "中国云仓");
        productNameMap.put("cwhsea", "极兔速递-东南亚云仓");
        productNameMap.put("ymsmy", "马来纭毅");
        productNameMap.put("ymseg", "埃及纭毅");
        productNameMap.put("iam", "统一身份认证");
        productNameMap.put("bosum", "Bosum知识付费平台");
        //productNameMap.put("safe", "安全部门");
        //productNameMap.put("pt", "平台测试");
        productNameMap.put("cjsph", "菲律宾驰钧货运平台");
        //productNameMap.put("db", "数据库运维");
        productNameMap.put("cjscn", "中国驰均货运平台");
        //productNameMap.put("cwscn", "中国云仓管理中台(废弃)");
        productNameMap.put("jtgroup", "集团");


       List<ApplicationCoverage> list = new ArrayList<>();
        String baseUrl = uri;
        String env = "uat";
        ApplicationCoverage applicationCoverage = null;
        for (String product : productNameMap.keySet()) {
            String fullUrl = String.format("%s?product=%s&env=%s", baseUrl, product, env);

            URL url = new URL(fullUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            // 设置请求方法和请求头
            connection.setRequestMethod("GET");
            connection.setRequestProperty("Authorization", Authorization);
            connection.setRequestProperty("Accept", "application/json");

            // 读取响应内容
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String inputLine;
            StringBuilder responseContent = new StringBuilder();
            while ((inputLine = in.readLine()) != null) {
                responseContent.append(inputLine);
            }
            in.close();

            JSONObject root = JSON.parseObject(responseContent.toString());
            JSONArray dataArray = root.getJSONArray("data");



            for (int i = 0; i < dataArray.size(); i++) {
                JSONObject item = dataArray.getJSONObject(i);
                if(StringUtils.isEmpty(item.getString("version"))){
                    continue;
                }

                JSONObject appInfo = item.getJSONObject("app_info");
                String name = appInfo.getString("name");
                if(name.toLowerCase().indexOf("-bg")==-1&&name.toLowerCase().indexOf("-job")==-1&&name.toLowerCase().indexOf("-app")==-1&&name.toLowerCase().indexOf("front")==-1&&name.toLowerCase().indexOf("ui")==-1&&name.toLowerCase().indexOf("h5")==-1&&name.contains("yl-")){//带-front的为前端应用，不需要
                    applicationCoverage = new ApplicationCoverage();
                    applicationCoverage.setAppName(name);//应用名称
                    applicationCoverage.setProduct(product);
                    applicationCoverage.setProductCn(productNameMap.get(product));
                    list.add(applicationCoverage);
                }
            }
        }



        return list;
    }

    public static void main(String[] args) throws IOException {
        String baseUrl = uri;
        String env = "uat";
        String product = "ymsvn";
        String fullUrl = String.format("%s?product=%s&env=%s", baseUrl, product, env);

        URL url = new URL(fullUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        // 设置请求方法和请求头
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Authorization", Authorization);
        connection.setRequestProperty("Accept", "application/json");

        // 读取响应内容
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String inputLine;
        StringBuilder responseContent = new StringBuilder();
        while ((inputLine = in.readLine()) != null) {
            responseContent.append(inputLine);
        }
        in.close();

        JSONObject root = JSON.parseObject(responseContent.toString());
        JSONArray dataArray = root.getJSONArray("data");

        System.out.println("dataArray:"+dataArray.toString());
    }




}

